/*
 * Sandora, a Falling Sand Game for the Pandora
 * Copyright (C) 2011  WaveHack <email@wavehack.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include <time.h>
#include "particle.h"

#define SANDORA_VERSION "0.1-alpha"

SDL_Surface *screen;

// Redirect output from stdout.txt to console
#undef main

int main(int argc, char *argv[]) {
#ifdef PANDORA
	if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_JOYSTICK) < 0) {
#else
	if (SDL_Init(SDL_INIT_VIDEO) < 0) {
#endif // PANDORA
		printf("SDL initialization failed: %s\n", SDL_GetError());
		return 1;
	}
	atexit(SDL_Quit);

#ifdef PANDORA
	screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 16, SDL_SWSURFACE|SDL_FULLSCREEN);
#else
	screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 16, SDL_HWSURFACE);
#endif // PANDORA
	if (screen == NULL) {
		printf("Unable to set video mode: %s\n", SDL_GetError());
		return 1;
	}

// Pandora is always fullscreen
#ifdef PANDORA
	SDL_ShowCursor(SDL_DISABLE);
#else
#ifdef WIN32
	SDL_WM_SetCaption("Sandora " SANDORA_VERSION " for Windows", NULL);
#endif // WIN32
#endif // PANDORA

	srand(time(NULL));

	initParticles();
	clearParticleScreen();

	bool done = false;
	SDL_Event event;

	Uint32 currentTicks;
	Uint32 lastTicks = SDL_GetTicks();
	int fps;
	int frames = 0;

	while (!done) {
		currentTicks = SDL_GetTicks();
		if (currentTicks - lastTicks > 1000) {
			fps = frames * 1000 / (currentTicks - lastTicks);
			lastTicks = currentTicks;
			frames = 0;
			printf("fps: %d\n", fps);
		}

		while (SDL_PollEvent(&event)) {
			if (event.type == SDL_QUIT)
				done = true;

			if (event.type == SDL_KEYDOWN) {
				if ((event.key.keysym.sym == SDLK_ESCAPE) || (event.key.keysym.sym == SDLK_q))
					done = true;

				if ((event.key.keysym.sym == SDLK_a) && (currentParticleType < MAX_PARTICLE_TYPES))
					currentParticleType++;

				if ((event.key.keysym.sym == SDLK_z) && (currentParticleType > 0))
					currentParticleType--;
			}

			if (event.type == SDL_MOUSEBUTTONDOWN) {
				SDL_MouseButtonEvent mbe = event.button;
				addParticles(mbe.x, mbe.y, 8);
			}

			if (event.type == SDL_MOUSEMOTION) {
				SDL_MouseMotionEvent mme = event.motion;

				if (mme.state & SDL_BUTTON(1))
					addParticles(mme.x, mme.y, 8);
			}

			//
		}
	
		updateParticles();

		SDL_FillRect(screen, NULL, 0);
		drawParticles();
		SDL_Flip(screen);

		frames++;
	}

	deinitParticles();
	SDL_Quit();

	return 0;
}
